package com.mxdx.edu.conf;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.mxdx.edu.common.ApiResult;
import com.mxdx.edu.common.RestApiException;
import com.mxdx.edu.common.WebAppException;
import com.mxdx.edu.common.log.AppLogger;
import com.mxdx.edu.constant.StaticConstants;
import com.mxdx.edu.enums.ErrorEnums;
import com.mxdx.edu.util.StringUtils;

/**
 * 统一错误处理
 * 
 * @author zhangyanchun
 * @date 2016-08-02
 */
@ControllerAdvice
class DefineExceptionHandler {

    private AppLogger logger = new AppLogger(DefineExceptionHandler.class);



    @ExceptionHandler(value = WebAppException.class)
    public ModelAndView webMvcErrorHandler(HttpServletRequest req, WebAppException ex) {

        logger.doError(String.format(StaticConstants.ERROR_MSG_FORMAT, ex.getMessage()), ex);
        ModelAndView mav = new ModelAndView();
        mav.addObject(StaticConstants.EXCEPTION, ex);
        mav.addObject(StaticConstants.STATUS, ex.getErrorCode());
        mav.addObject(StaticConstants.URI, req.getServletPath());
        mav.setViewName("common/error");
        return mav;
    }

    @ExceptionHandler(value = RestApiException.class)
    @ResponseBody
    public ApiResult<Map<String, Object>> jsonErrorHandler(HttpServletRequest req,
            RestApiException re) {
        logger.doError(String.format(StaticConstants.ERROR_MSG_FORMAT, re.getMessage()), re);
        ApiResult<Map<String, Object>> ret = new ApiResult<>();
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(re.getNotice())) {
            ret.setMessage(re.getNotice());
        } else {
            ret.setMessage(re.getMessage());
        }
        ret.setStatus(re.getErrorCode());
        ret.setApiName(StaticConstants.API_NAME);
        map.put(StaticConstants.DATA, String.format("error:%s", req.getServletPath()));
        ret.setResult(map);
        return ret;
    }

    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception ex) {
        logger.doError(String.format(StaticConstants.ERROR_MSG_FORMAT, ex.getMessage()), ex);
        ModelAndView mav = new ModelAndView();
        mav.addObject(StaticConstants.EXCEPTION, ex);
        mav.addObject(StaticConstants.URI, req.getServletPath());
        if (!StringUtils.isEmpty(req.getHeader("X-Requested-With"))) {// 请求为json方式
            ApiResult<Map<String, Object>> ret = new ApiResult<>();
            Map<String, Object> map = new HashMap<>();
            ret.setStatus(ErrorEnums.SYSTEM_ERROR.getCode());
            ret.setApiName(StaticConstants.API_NAME);
            map.put(StaticConstants.DATA, String.format("error:%s", req.getServletPath()));
            ret.setResult(map);
            String exc = JSON.toJSONString(ret);
            mav.addObject(StaticConstants.EXCEPTION, exc);
            // 页面只包含异常字符串
            mav.setViewName("error");
        } else {
            mav.setViewName("404");
        }
        return mav;
    }


}
